//
//  PlayerBottomView.m
//  MyVocalNovels
//
//  Created by mac on 2019/4/17.
//  Copyright © 2019 YangYang. All rights reserved.
//

#import "PlayerBottomView.h"
#import "AlbumModel.h"

@interface PlayerBottomView ()

@property (nonatomic, strong) UIImageView *albumImage;
@property (nonatomic, strong) UISlider *slider;
@property (nonatomic, strong) UIButton *lastMusicView;
@property (nonatomic, strong) UIButton *playView;
@property (nonatomic, strong) UIButton *nextMusicView;
@property (nonatomic, strong) UILabel *songNameView;
@property (nonatomic, strong) UILabel *singerNameView;

@end

@implementation PlayerBottomView

static PlayerBottomView *instance = nil;

+(id)shareInstance
{
    
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        instance = [[super alloc] init];
    });
    return instance;
}

+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    NSLog(@"allocWithZone");
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [super allocWithZone:zone];
    });
    return instance;
//    return [PlayerBottomView shareInstance];
}

-(instancetype)copyWithZone:(NSZone *)zone
{
    NSLog(@"copyWithZone");
    return instance;
}

-(instancetype)mutableCopyWithZone:(NSZone *)zone
{
    NSLog(@"mutableCopyWithZone");
    return instance;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        NSLog(@"initWithFrame");
        self.backgroundColor = [UIColor blueColor];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction)];
        [self addGestureRecognizer:tapGesture];
        [self initUI];
    }
    return instance;
}

-(void)initUI
{
    NSLog(@"创建UI");
    _albumImage = [[UIImageView alloc] init];
    _albumImage.layer.cornerRadius = 40;
    [self addSubview:_albumImage];
    [_albumImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).mas_offset(10);
        make.centerY.mas_equalTo(self.mas_centerY).mas_offset(0);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
    }];
    _slider = [[UISlider alloc] init];
    [self addSubview:_slider];
    [_slider addTarget:self action:@selector(sliderActionMethod:) forControlEvents:UIControlEventValueChanged];
    [_slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.albumImage.mas_right).mas_offset(10);
        make.right.mas_equalTo(self.mas_right).mas_offset(-10);
        make.top.mas_equalTo(self.mas_top).mas_offset(0);
        make.height.mas_equalTo(40);
    }];
    
    _playView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_playView];
    [_playView setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
    [_playView setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateSelected];
    [_playView addTarget:self action:@selector(playingActionMethod) forControlEvents:UIControlEventTouchUpInside];
    [_playView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.slider.mas_bottom).mas_offset(0);
        make.centerX.mas_equalTo(self.slider.mas_centerX).mas_offset(0);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
    _nextMusicView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_nextMusicView];
    [_nextMusicView setImage:[UIImage imageNamed:@"下一首"] forState:UIControlStateNormal];
    [_nextMusicView addTarget:self action:@selector(nextMusicActionMethod) forControlEvents:UIControlEventTouchUpInside];
    [_nextMusicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.playView.mas_right).mas_offset(20);
        make.top.mas_equalTo(self.slider.mas_bottom).mas_offset(0);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
    _lastMusicView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_lastMusicView];
    [_lastMusicView setImage:[UIImage imageNamed:@"上一首"] forState:UIControlStateNormal];
    [_lastMusicView addTarget:self action:@selector(lastMusicActionMethod) forControlEvents:UIControlEventTouchUpInside];
    [_lastMusicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.playView.mas_left).mas_offset(-20);
        make.centerY.mas_equalTo(self.playView.mas_centerY).mas_offset(0);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
    _songNameView = [[UILabel alloc] init];
    [self addSubview:_songNameView];
    _songNameView.font = [UIFont systemFontOfSize:10];
    [_songNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.slider.mas_left).mas_offset(0);
        make.right.mas_equalTo(self.lastMusicView.mas_left).mas_offset(0);
        make.top.mas_equalTo(self.slider.mas_bottom).mas_offset(5);
    }];
    
    _singerNameView = [[UILabel alloc] init];
    [self addSubview:_singerNameView];
    _singerNameView.font = [UIFont systemFontOfSize:10];
    [_singerNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.songNameView.mas_left).mas_offset(0);
        make.right.mas_equalTo(self.lastMusicView.mas_left).mas_offset(0);
        make.top.mas_equalTo(self.songNameView.mas_bottom).mas_offset(10);
    }];
    
    _albumImage.image = [UIImage imageNamed:@"album_3.jpg"];
}

-(void)tapGestureAction
{
    if (self.ClickViewBlock) {
        self.ClickViewBlock(self);
    }
//    self.ClickViewBlock();
}

-(void)playingActionMethod
{
    if (_model) {
        MusicPlayingViewController *musicPlayingVC = [MusicPlayingViewController sharedInstance];
        [musicPlayingVC playingActionMethod];
    }
    
}

-(void)nextMusicActionMethod
{
    if (_model) {
        MusicPlayingViewController *musicPlayingVC = [MusicPlayingViewController sharedInstance];
        [musicPlayingVC nextMusicActionMethod];
    }
    
}

-(void)lastMusicActionMethod
{
    if (_model) {
        MusicPlayingViewController *musicPlayingVC = [MusicPlayingViewController sharedInstance];
        [musicPlayingVC lastMusicActionmethod];
    }
}

-(void)sliderActionMethod:(UISlider *)sender
{
    if (_model) {
        MusicPlayingViewController *musicPlayingVC = [MusicPlayingViewController sharedInstance];
        [musicPlayingVC sliderActionMethod:sender];
    }
    else
    {
        _slider.value = 0;
    }
}

-(void)setModel:(AlbumModel *)model
{
    _model = model;
    _songNameView.text = model.songName;
    _singerNameView.text = model.singerName;
    _albumImage.image = [UIImage imageNamed:model.albumImage];
}

-(void)setIsPlaying:(BOOL)isPlaying
{
    _isPlaying = isPlaying;
    _playView.selected = isPlaying;
}

-(void)setProgress:(CGFloat)progress
{
    _progress = progress;
    _slider.value = progress;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
